import { defineStore } from 'pinia'
import router from '@/router/index.js'
import { setToken } from '@/router/utils/tokenConfig'
import { routerSave, routerClose, AllClose, routerNext } from '@/utils/routerConfig'
import { sessionSet } from '@/utils/sessionStorage'

const useStore = defineStore('storeId', {
  // 为了完整类型推理，推荐使用箭头函数
  state: () => {
    return {
      // 所有这些属性都将自动推断出它们的类型
      token: '',
      routerList: [],
      usersInfo: {}
    }
  },
  actions: {
    getToken(token) {
      this.token = token
      setToken(token)
      router.push('/')
    },
    getUsersInfo(data) {
      this.usersInfo = data
      sessionSet('usersInfo', data)
    },
    removeToken() {
      sessionStorage.removeItem('token')
    },
    routerSavePinia(route) {
      this.routerList = routerSave(route)
      return this.routerList
    },
    routerRemovePinia(route) {
      this.routerList = routerClose(route)
      return routerNext()
    },
    routerAllClosePinia() {
      this.routerList = AllClose()
    }
  }
})

export default useStore
